package android.Graphics;

import javax.microedition.khronos.opengles.GL10;
import java.nio.FloatBuffer;

/**
 * Created by IntelliJ IDEA.
 * User: akuryou
 * Date: 4/26/11
 * Time: 12:40 PM
 * To change this template use File | Settings | File Templates.
 */
public class InitStar
{
    private float[] star;
    private int x;
    private int y;
    private int cellWidth;
    private int cellHeight;
    private int lineWidth;

    private FloatBuffer vertexBuffer;

    public InitStar( int x, int y, int cellWidth, int cellHeight, int lineWidth )
    {
        this.x = x;
        this.y = y;
        this.cellWidth = cellWidth;
        this.cellHeight = cellHeight;
        this.lineWidth = lineWidth;
        initStar();
    }

    public void initStar()
    {
        star = starArray( x, y, cellWidth, cellHeight, lineWidth );
        //System.out.println("initStar() " + x + "\t" + y);
        vertexBuffer = FloatBuffer.allocate( star.length );
        vertexBuffer = FloatBuffer.wrap( star );
        vertexBuffer.put( star );
        vertexBuffer.position( 0 );
    }

    public float[] starArray( int x, int y, int cellWidth, int cellHeight, int lineWidth )
    {
        star = new float[10];
        //Top
        star[0] = x * cellWidth + cellWidth/2;
        star[1] = (y+1) * cellHeight - 3*lineWidth;
        //Bottom Left
        star[2] = x * cellWidth + 3*lineWidth;
        star[3] = y * cellHeight + 3*lineWidth;
        //Right
        star[4] = (x+1) * cellWidth - lineWidth;
        star[5] = y * cellHeight + (13*cellHeight)/22;
        //Left
        star[6] = x * cellWidth + lineWidth;
        star[7] = star[5];
        //Bottom right
        star[8] = star[4] - lineWidth;
        star[9] = star[3];

        return star;
    }

    public void drawStar( GL10 gl )
    {
        gl.glVertexPointer( 2, GL10.GL_FLOAT, 0, vertexBuffer );
        gl.glEnableClientState( GL10.GL_VERTEX_ARRAY );
        gl.glDrawArrays( GL10.GL_LINE_LOOP, 0, star.length/2 );
    }
}
